import logging

from mysql_pg_compare import DatabaseComparator, DBConfig

if __name__ == "__main__":
    # 配置示例
    config = DBConfig(
        mysql_host="172.16.1.27",
        mysql_port=3306,
        mysql_user="root",
        mysql_password="hmgf@600987",
        mysql_database="hm_erp",
        pg_host="172.16.1.21",
        pg_port=31003,
        pg_user="postgres",
        pg_password="postgres",
        pg_database="upms"
    )

    comparator = DatabaseComparator(config)

    if not comparator._connect_mysql() or not comparator._connect_pg():
        exit(1)

    # 结构比对
    schema_diff = comparator.compare_schemas()
    logging.info(f"结构差异: {schema_diff}")

    # 数据比对（示例比对orders表）
    data_diff = comparator.compare_table_data("releases", pk_column="id")
    if data_diff.get('error') is not None and data_diff['error'] != '':
        logging.info(f"无法对比，错误原因: {data_diff['error']}")
        exit(-1)
    logging.info(f"数据差异数量: {data_diff['total']}")

    # 输出差异到文件
    with open('differences.json', 'w') as f:
        import json

        json.dump({
            'schema': schema_diff,
            'data': data_diff
        }, f, indent=2)